package com.mycms.module.extjs.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mycms.domain.FileInfo;

@Controller
class ExtjsController {
	
	/**
	 * extjs tree (디렉토리 구조를 tree로 표현)
	 * 
	 */
	@RequestMapping(value = "/admin/tree1.do", method = RequestMethod.GET)
	public String tree1(){
		return "extjs/tree1";
	}
		
	/**
	 * extjs tree json data
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/admin/json/getNodes.do", method = RequestMethod.GET)
	@ResponseBody
	public List<FileInfo> getNodes(HttpServletRequest request) {

		String baseDirectory = request.getSession().getServletContext().getRealPath("/") + "design/resource/js/extjs";

		File file = new File(baseDirectory);

		List<FileInfo> resultList = new ArrayList<FileInfo>();
		FileInfo fileInfo = new FileInfo();
		fileInfo.setText(file.getName());
		fileInfo.setId("Ext" + file.getPath().replace(baseDirectory.replace("/", "\\"), ""));
		fileInfo.setCls("folder");
		return getFileChildren(file, fileInfo, baseDirectory, resultList, true);
	}
	
	/**
	 * 디렉토리 구조를 extjs tree의 json 데이터 형식에 맞는 List로 추출
	 * 
	 * @param file
	 * @param fileInfo
	 * @param baseDirectory
	 * @param resultList
	 * @param isFirst
	 * @return
	 */
	public List<FileInfo> getFileChildren(File file, FileInfo fileInfo, String baseDirectory, List<FileInfo> resultList, Boolean isFirst) {

		if (file.isDirectory()) {

			File[] fileList = file.listFiles();
			List<FileInfo> childrenList = new ArrayList<FileInfo>();
			for (int i = 0; i < fileList.length; i++) {

				FileInfo childrenFileInfo = new FileInfo();
				childrenFileInfo.setText(fileList[i].getName());
				childrenFileInfo.setId("Ext" + fileList[i].getPath().replace(baseDirectory.replace("/", "\\"), ""));

				if (fileList[i].isFile()) {
					childrenFileInfo.setLeaf(true);
					childrenFileInfo.setCls("file");
				} else {
					getFileChildren(fileList[i], childrenFileInfo, baseDirectory, resultList, false);
					childrenFileInfo.setCls("folder");
				}

				childrenList.add(childrenFileInfo);
				childrenFileInfo = null;
			}

			fileInfo.setChildren(childrenList);
			if (isFirst) {
				resultList.add(fileInfo);
			}
		}

		return resultList;
	}
	
	/**
	 * extjs grid1
	 * 
	 */
	@RequestMapping(value = "/admin/grid1.do", method = RequestMethod.GET)
	public String grid1(){
		return "extjs/grid1";
	}
	
	/**
	 * extjs grid1 json data
	 * 
	 */
	@RequestMapping(value="/admin/json/grid1Data.do", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> grid1Data(@RequestParam(required=false) int page) {
		
		int pageNum = 10;
		int start = (page-1)*pageNum+1;
		
		List<FileInfo> resultList = new ArrayList<FileInfo>();
		for(int i = start ; i < start+10 ; i++) {
			FileInfo fileInfo = new FileInfo();
			fileInfo.setId("id-" + i);
			fileInfo.setText(i + "번 파일");
			fileInfo.setCls("class" + i);
			
			resultList.add(fileInfo);
		}
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("results", resultList);
		resultMap.put("totalCount", new Integer(25));
		return resultMap;
	}
	
}
